package com.anuo.app.modules.system.service;

import com.anuo.app.common.thirdparty.PictureUtil;
import com.anuo.app.modules.system.dao.FileinfoDao;
import com.anuo.app.modules.system.entity.Fileinfo;
import com.anuo.app.modules.system.entity.SystemFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class FileService {
    @Autowired
    FileinfoDao fileinfoDao;

    @Transactional(readOnly = false)
    public void insertFileinfo(Fileinfo fileinfo) {
        fileinfoDao.insertFileinfo(fileinfo);
    }

    @Transactional(readOnly = false)
    public void saveFileinfoList(List<Fileinfo> fileinfoList, String masterType, String masterid) {
        //先删除
        SystemFilter systemFilter=new SystemFilter();
        systemFilter.setMaster_id(masterid);
        fileinfoDao.deleteFileinfo(systemFilter);
        //后添加
        for (Fileinfo fileinfo : fileinfoList) {
            fileinfo.setMaster_type(masterType);
            fileinfo.setMaster_id(masterid);
            fileinfo.preInsert();
            fileinfo.setFilename(fileinfo.getFilename());
            fileinfo.setFilepath("");
            fileinfo.setSize(0L);
            fileinfoDao.insertFileinfo(fileinfo);
        }
    }

    @Transactional(readOnly = true)
    public void getFileinfoCloudUrlList(List<Fileinfo> fileinfoList, int width, int high) {
        for (Fileinfo fileinfo : fileinfoList) {
            fileinfo.setFilepath(PictureUtil.getPictureUrl(fileinfo.getFilename(), width, high));
        }
    }

    @Transactional(readOnly = true)
    public List<Fileinfo> getFileinfoList(SystemFilter systemFilter) {
        return fileinfoDao.getFileinfoList(systemFilter);
    }

    @Transactional(readOnly = false)
    public void deleteFileinfo(SystemFilter systemFilter) {
        fileinfoDao.deleteFileinfo(systemFilter);
    }


}
